package com.itheima.web;

import com.itheima.domain.PageBean;
import com.itheima.domain.User;
import com.itheima.service.IUserService;
import com.itheima.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*//0.请求体乱码问题,request中会出现中文
        request.setCharacterEncoding("utf-8");*/
        //1.1获取request请求中的currentPage
        String currentPage = request.getParameter("currentPage");//当前页码
        //1.2获取request请求中的rows
        String rows = request.getParameter("rows");//每一页展示数量

        //1.3获取请求体中的所有参数
        Map<String, String[]> condition = request.getParameterMap();

        //2.先设置默认值
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }if(rows==null||"".equals(rows)){
            rows="5";
        }

        //3.调用service方法将获得到的currentPage和row存入到PageBean对象中
        IUserService service = new UserServiceImpl();
        //获得查询条件
        PageBean<User> pb =service.findUserByPage(currentPage,rows,condition);
        // System.out.println("pb = " + pb);

        //4.将pageBean存入到request域中
        request.setAttribute("pb",pb);

        //5.将条件放入request中
        request.setAttribute("condition",condition);
        request.getRequestDispatcher("/list.jsp").forward(request,response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
